import Vue from 'vue'
import App from './App.vue'
import router from './router'

import {tokenUrl} from './valid/tokenUrl'

import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios, axios)

import 'lib-flexible/flexible'

import {
  Button,
  Tabbar, 
  TabbarItem,
  Search,
  Swipe,
  SwipeItem,
  Lazyload,
  Tab,
  Tabs,
  Sidebar,
  SidebarItem,
  NavBar,
  Icon,
  Toast,
  Form,
  Field,
  SubmitBar,
  Checkbox,
  CheckboxGroup,
  SwipeCell,
  Card,
  Notify,
  Dialog,
  Empty,
  Stepper,
  AddressList,
  AddressEdit,
  Area,
  Tag,
  Cell,
  CellGroup,
  Grid,
  GridItem,
  Uploader,
  Image as VanImage,
  } from 'vant';

Vue
  .use(Button)
  .use(Tabbar)
  .use(TabbarItem)
  .use(Search)
  .use(Swipe)
  .use(SwipeItem)
  .use(Lazyload)
  .use(Lazyload, {
    lazyComponent: true,
  })
  .use(Tab)
  .use(Tabs)
  .use(Sidebar)
  .use(SidebarItem)
  .use(NavBar)
  .use(Icon)
  .use(Toast)
  .use(Form)
  .use(Field)
  .use(SubmitBar)
  .use(Checkbox)
  .use(CheckboxGroup)
  .use(SwipeCell)
  .use(Card)
  .use(Notify)
  .use(Dialog)
  .use(Empty)
  .use(Stepper)
  .use(AddressList)
  .use(AddressEdit)
  .use(Area)
  .use(Tag)
  .use(Cell)
  .use(CellGroup)
  .use(Grid)
  .use(GridItem)
  .use(Uploader)
  .use(VanImage)
  

axios.defaults.baseURL = 'http://www.kangliuyong.com:10002';

//axios请求之前拦截器
axios.interceptors.request.use(config=>{
  // config:请求配置(请求头、请求参数、请求类型、请求路径...)

  let tokenString = localStorage.getItem('token');

  let appkey = 'U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA=';

  if(config.method == 'get'){
    config.params = config.params || {};
    config.params.appkey = appkey;

    if(tokenUrl.indexOf(config.url) > -1){
      config.params.tokenString = tokenString
    }

  }else if(config.method == 'post'){
    config.data = config.data || {};

    if(tokenUrl.indexOf(config.url) > -1){
      config.data.tokenString = tokenString
    }

    //序列化post请求参数
    let dataParams = ''
    for(let key in config.data){
      dataParams += `${key}=${config.data[key]}&`
    }
    dataParams += `appkey=${appkey}`
    config.data = dataParams
  }

  return config
},err=>{
  console.log(err);

  return Promise.reject(err)
})

Vue.config.productionTip = false

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
